package com.ruoyi.business.rabbitmq.exchanger;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.ruoyi.business.rabbitmq.AcquisitionProtocolConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataReceiveExchanger {

    @Bean
    public String dataFanoutExchange(@Qualifier("firstConnectionFactory") ConnectionFactory firstConnectionFactory, @Qualifier("secondConnectionFactory") ConnectionFactory secondConnectionFactory){
        initQueueExchangeConfig(firstConnectionFactory);
        initQueueExchangeConfig(secondConnectionFactory);
        return AcquisitionProtocolConstants.DATA_EXCHANGER_NAME;
    }

    private void initQueueExchangeConfig(ConnectionFactory connectionFactory) {
        try {
            Channel channel = connectionFactory.createConnection().createChannel(false);
            channel.queueDeclare(AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_LOCAL, true, false, false, null);
            channel.queueDeclare(AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_REMOTE, true, false, false, null);
            channel.exchangeDeclare(AcquisitionProtocolConstants.DATA_EXCHANGER_NAME, BuiltinExchangeType.FANOUT, true);
            channel.queueBind(AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_LOCAL, AcquisitionProtocolConstants.DATA_EXCHANGER_NAME, "", null);
            channel.queueBind(AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_REMOTE, AcquisitionProtocolConstants.DATA_EXCHANGER_NAME, "", null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
